/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.client.inGame;
import core.messages.client.inGame.Msg_Card_Cast;
import core.messages.client.inGame.Msg_Card_Cast_Phase_End;
import core.threads.client.Client_Public_Data;
import core.threads.client.Client_Public_MsgBox;
import core.threads.client.Client_Public_Thread;
import java.util.ArrayList;
import java.util.List;
import models.CastInfo;
import models.Player;
import myTest.MYTest;

/**
 *
 * @author Invince
 */
public class T_Client_Cast_Phase  extends Thread{
/*----------Attributs-------------------*/
//    protected boolean stop;
//    protected boolean suspend;

/*----------Public functions------------*/
    @Override
    public void run() {
        /**-----Init-----**/
        String name;
        synchronized(Client_Public_Data.semaMyInfo){
            name=Client_Public_Data.myInfo.getName();
        }
        /**-----Main-----**/
        boolean isCastOver;
        synchronized(Client_Public_Data.semaIsCastOver){
            isCastOver=Client_Public_Data.isCastOver;
        }
        if(!isCastOver){
            boolean isLBSS;
            synchronized(Client_Public_Data.semaJudgeResult){
                isLBSS=Client_Public_Data.judgeResult.isLBSS();
            }
            if(!isLBSS){
                boolean castable=false;
                CastInfo castInfo = null;
                while(!castable){
                    while(Client_Public_MsgBox.msgBox_Client_Card_toCast.isEmpty()){}
                    castInfo=Client_Public_MsgBox.msgBox_Client_Card_toCast.poll();//??????????这以下还得考虑
                    castable=isCastable(castInfo);
                    if(!castable){
                        MYTest.println("You can't cast this card or skill!");
                    }
                }
                List<String> nameTargets=new ArrayList<String>();
                for(Player player: castInfo.getTargetPlayers()){
                    nameTargets.add(player.getName());
                }
                Msg_Card_Cast msgCardCast=new Msg_Card_Cast(name, "Server",castInfo);
                Client_Public_MsgBox.msgBox_Client_Send_Msg.offer(msgCardCast);
                if(castInfo.getSkill().getName().equals("杀")){//记录是否出了杀，（注意该信息在轮次开始时更新）
                    synchronized(Client_Public_Data.semaIsShaCasted){
                        Client_Public_Data.isShaCasted=true;
                    }
                }
            }else{
                synchronized(Client_Public_Data.semaIsCastOver){
                    Client_Public_Data.isCastOver=true;
                }
                Msg_Card_Cast_Phase_End msgCardCastPhaseEnd=new Msg_Card_Cast_Phase_End(name, "Server");
                Client_Public_MsgBox.msgBox_Client_Send_Msg.offer(msgCardCastPhaseEnd);
                Client_Public_Thread.tClientDropPhase=new T_Client_Drop_Phase();
                Client_Public_Thread.tClientDropPhase.start();
            }
        /**----End-----**/
        }else{
            Msg_Card_Cast_Phase_End msgCardCastPhaseEnd=new Msg_Card_Cast_Phase_End(name, "Server");
            Client_Public_MsgBox.msgBox_Client_Send_Msg.offer(msgCardCastPhaseEnd);
            Client_Public_Thread.tClientDropPhase=new T_Client_Drop_Phase();
            Client_Public_Thread.tClientDropPhase.start();
        }
    }

//    public void stopT(){
//        synchronized(this){
//            this.stop=true;
//        }
//    }
//
//    public void suspendT(){
//        synchronized(this){
//            this.suspend=true;
//        }
//    }
//
//    public void resumeT(){
//        synchronized(this){
//            this.suspend=false;
//        }
//    }
/*----------Private functions-----------*/
    private boolean isCastable(CastInfo castInfo){//？？？？？？这个还未完成
        boolean isShaCasted;
        synchronized(Client_Public_Data.semaIsShaCasted){
            isShaCasted=Client_Public_Data.isShaCasted;
        }
        Player myInfo;
        synchronized(Client_Public_Data.semaMyInfo){
            myInfo=Client_Public_Data.myInfo;
        }
        
        if(isShaCasted){
            if(castInfo.getSkill().getName().equals("杀")&&
                  !(
                       (myInfo.getRole().getName().equals("张飞"))||
                       (myInfo.getDeckEqp().getWeapon().getName().equals("诸葛弩"))
                   )
            ){//出了杀，又不是张飞，也没装备诸葛弩
                return false;
            }
        }
        return false;
    }
/*----------Constructor-----------------*/
    public T_Client_Cast_Phase() {
        synchronized(this){
//            this.stop=false;
//            this.suspend=false;
        }
    }
/*----------Gets and sets---------------*/
}
